<?php
/**
 * DSN
 *
 * @package util.db
 * @since 0.2
 */
class IDsn extends IObject {
	private $options = array();
	private $driverName;
	
	/**
	 * 构造器
	 *
	 * @param string $dsn DSN字符串
	 * @since 0.2
	 */
	public function __construct($dsn) {
		if (!strstr($dsn, ":")) {
			throw new IDbException("unknown data source");
		}
		list($this->driverName, $property) = explode(":", $dsn, 2);
		$options = explode(";", $property);
		foreach ($options as $option) {
			list($key, $value) = explode("=", $option, 2);
			$this->options[trim($key)] = trim($value);
		}
	}
	
	/**
	 * 取得选项
	 *
	 * @return array
	 * @since 0.2
	 */
	public function getOptions() {
		return $this->options;
	}
	
	/**
	 * 将当前DSN配置导入到新的配置中
	 *
	 * @param array $config 新的配置
	 */
	public function dumpTo(array &$config) {
		$config = array_merge($config, $this->options);
	}
	
	/**
	 * 取得对应的驱动
	 *
	 * @param array $config 配置
	 * @return IDbDriver
	 */
	public function getDriver($config) {
		return IDb::getDriver($this->driverName, $config);
	}
}

?>